//
//  RMMHuLiDetailsViewController.m
//  Romomo
//
//  Created by puman on 2017/1/4.
//  Copyright © 2017年 mazegong. All rights reserved.
//

#import "RMMHuLiDetailsViewController.h"
#import "RMMMyHuLiDetailsHeadView.h"
#import "RMMHuLiDetailsTableViewCell.h"
#import "RMMTuiKuanViewController.h"
#import "RMMSelcetPickerView.h"
#import "RMMGoodsConfirmViewController.h"
#import "RMMShopingModel.h"

@interface RMMHuLiDetailsViewController ()<UITableViewDelegate, UITableViewDataSource>

@end

@implementation RMMHuLiDetailsViewController
{
    RMMMyHuLiDetailsHeadView *_headView;
    RMMSelcetPickerView *_selcetPickerView;
    NSString *_date;
    UIView *_yanQView;
    UITableView *_tableView;
    NSArray *_dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kCColor;
    [self addSubViews];
    [self getData];
    //[self addRightItem];
    self.navigationItem.title = @"护理详情";
    // Do any additional setup after loading the view.
}
- (void)getData{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"org_no"] = Org_no;
    parDic[@"cust_id"] = [RMMFileManager shareHandle].userDic[@"CUST_ID"];
    parDic[@"my_cust_id"] = _model.MY_CARD_ID;
    NSString *url = GetTaoCanUsedUrl;
    [OSRequestManager requestWithUrlString:url parDic:parDic network:^(BOOL isNetwork) {
    } method:GET finish:^(NSDictionary *dataDic) {
        NSString *status = [NSString stringWithFormat:@"%@",dataDic[@"status"]];
        if ([status isEqualToString:@"1"]) {
            _dataArray = dataDic[@"data"];
        } else {
            [RMMFileManager MBProgressWith:dataDic[@"message"] onView:self.view];
        }
        [_tableView reloadData];
    } error:^(NSError *errors) {
        
    } view:self.view];

}
- (void)addSubViews{
    NSString *time = [RMMFileManager retrunObjWith:_model.END_DATE];
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:3600 * 24 * 30];
    NSString *string = [inputFormatter stringFromDate:date];
    _headView = [[RMMMyHuLiDetailsHeadView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, 235 + (kScreenWideth - 50))];
    if (![_model.IS_DELAY isEqualToString:@"1"]) {
        if ([time compare:string] == NSOrderedAscending) {
            // 即将到期
            _headView.isHide = NO;
            BUTTON_S(_headView.yanQBut, yanqi);
            _headView.frame = CGRectMake(0, 0, kScreenWideth, 235 + (kScreenWideth - 50));
        } else {
            _headView.isHide = YES;
            _headView.frame = CGRectMake(0, 0, kScreenWideth, 155 + (kScreenWideth - 50));
        }
    } else {
        _headView.isHide = YES;
        _headView.frame = CGRectMake(0, 0, kScreenWideth, 155 + (kScreenWideth - 50));
    }
    BUTTON_S(_headView.bookingBut, bookingBut);
    _headView.model = _model;
    [self.view addSubview:_headView];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, kScreenHeight - 64) style:(UITableViewStylePlain)];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.tableHeaderView = _headView;
    [_tableView registerClass:[RMMHuLiDetailsTableViewCell class] forCellReuseIdentifier:@"Cell"];
    [self.view addSubview:_tableView];
    //[self addRightItem];
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, 0)];
    _selcetPickerView = [[RMMSelcetPickerView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, kScreenHeight - 64)];
    BUTTON_S(_selcetPickerView.rihthBut, doneButClick);
    _selcetPickerView.hidden = YES;
    _selcetPickerView.type = @"date";
    [self.view addSubview:_selcetPickerView];

}
- (void)bookingBut{
    if ([_model.PROD_SERVICE_TYPE isEqualToString:@"1"]) {
        [RMMFileManager MBProgressWith:@"辅助项目不能单独购买!" onView:self.view];
        return;
    }
    RMMGoodsConfirmViewController *VC = [[RMMGoodsConfirmViewController alloc] init];
    VC.goodsArray = [NSMutableArray array];
    RMMShopingModel *taoModel = [[RMMShopingModel alloc] init];
    taoModel.IMAGEURL = _model.SER_URL;
    taoModel.SERVICE_ID = [NSString stringWithFormat:@"%@",_model.SERVER_ID];
    taoModel.PRICE = [NSString stringWithFormat:@"%@",_model.PRICE];
    taoModel.COUNT = @1;;
    taoModel.SERVICE_NAME = _model.NAME;
    taoModel.MY_CARD_ID = [NSString stringWithFormat:@"%@",_model.MY_CARD_ID];
    taoModel.TIME_LEN = _model.TIME_LEN;
    [VC.goodsArray addObject:taoModel];
    VC.isHome = _model.IS_HOME;
    [self.navigationController pushViewController:VC animated:YES];
}
- (void)yanqi{
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *d = [inputFormatter dateFromString:_model.END_DATE];
    NSDate *endDate = [NSDate dateWithTimeInterval:30 * 24 * 60 * 60 sinceDate:d];
    
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"org_no"] = Org_no;
    parDic[@"bom_id"] = _model.BOM_ID;
    parDic[@"end_time"] = [inputFormatter stringFromDate:endDate];
    NSString *url = YanQiurl;
    [OSRequestManager requestWithUrlString:url parDic:parDic network:^(BOOL isNetwork) {
    } method:GET finish:^(NSDictionary *dataDic) {
        NSString *status = [NSString stringWithFormat:@"%@",dataDic[@"status"]];
        if ([status isEqualToString:@"1"]) {
            [MMProgressHUD show];
            [MMProgressHUD dismissWithSuccess:@"延期成功!" title:nil afterDelay:1.0];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                NS_POST(@"MUHUILIUPDATE", nil, nil);
                [self.navigationController popViewControllerAnimated:YES];
            });
        } else {
            [RMMFileManager MBProgressWith:dataDic[@"message"] onView:self.view];
        }
        [_tableView reloadData];
        
    } error:^(NSError *errors) {
        
    } view:self.view];

//    [_selcetPickerView.datePicker addTarget:self action:@selector(getDate:) forControlEvents:(UIControlEventValueChanged)];
//    _selcetPickerView.hidden = NO;
//    [UIView animateWithDuration:kInterval animations:^{
//        _selcetPickerView.view.frame = CGRectMake(0, kScreenHeight - 64 - 249, kScreenWideth, 249);
//        _selcetPickerView.backView.alpha = 1.0;
//    } completion:^(BOOL finished) {
//        
//    }];
}
- (void)getDate:(UIDatePicker *)picker{
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
    [outputFormatter setDateFormat:@"yyyy-MM-dd"];
    _date = [outputFormatter stringFromDate:picker.date];
}
- (void)doneButClick{
//    [UIView animateWithDuration:kInterval animations:^{
//        _selcetPickerView.view.frame = CGRectMake(0, kScreenHeight , kScreenWideth, 249);
//        _selcetPickerView.backView.alpha = 0.0;
//    } completion:^(BOOL finished) {
//        _selcetPickerView.hidden = YES;
//        NSDate *date = [[NSDate alloc] initWithTimeIntervalSinceNow:0];
//        NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
//        [outputFormatter setDateFormat:@"yyyy-MM-dd"];
//        NSString *time = [outputFormatter stringFromDate:date];
//        if (_date.length == 0) {
//            _date = time;
//        }
//        if ([time compare:_date] == NSOrderedDescending) {
//            [RMMFileManager MBProgressWith:@"时间选择错误!" onView:self.view];
//            return ;
//        }
//}];
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *d = [inputFormatter dateFromString:_model.END_DATE];
    NSDate *endDate = [NSDate dateWithTimeInterval:30 * 24 * 60 * 60 sinceDate:d];
    
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"org_no"] = Org_no;
    parDic[@"bom_id"] = _model.BOM_ID;
    parDic[@"end_time"] = [inputFormatter stringFromDate:endDate];
    NSString *url = YanQiurl;
    [OSRequestManager requestWithUrlString:url parDic:parDic network:^(BOOL isNetwork) {
    } method:GET finish:^(NSDictionary *dataDic) {
        NSString *status = [NSString stringWithFormat:@"%@",dataDic[@"status"]];
        if ([status isEqualToString:@"1"]) {
            [MMProgressHUD show];
            [MMProgressHUD dismissWithSuccess:@"延期成功!" title:nil afterDelay:1.0];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                NS_POST(@"MUHUILIUPDATE", nil, nil);
                [self.navigationController popViewControllerAnimated:YES];
            });
        } else {
            [RMMFileManager MBProgressWith:dataDic[@"message"] onView:self.view];
        }
        [_tableView reloadData];
        
    } error:^(NSError *errors) {
        
    } view:self.view];
}
- (void)addRightItem{
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"展开图标@3x"] style:(UIBarButtonItemStylePlain) target:self action:@selector(itemClick)];
    item.tintColor = RGBA(44, 44, 44, 1);
    self.navigationItem.rightBarButtonItem = item;
}
- (void)itemClick{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
    UIAlertAction *tiXianAction = [UIAlertAction actionWithTitle:@"查看详情" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *shuoMingAction = [UIAlertAction actionWithTitle:@"申请退款" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        RMMTuiKuanViewController *VC = [[RMMTuiKuanViewController alloc] init];
        [self.navigationController pushViewController:VC animated:YES];
    }];
    UIAlertAction *callAction = [UIAlertAction actionWithTitle:@"联系客服" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        [RMMFileManager callKeFuWithPhoneNumber:NumberSJ onView:self.view];
        
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    if (DEVICE >= 9.0) {
        [callAction setValue:kBasicColor forKey:@"titleTextColor"];
        [tiXianAction setValue:[UIColor blackColor] forKey:@"titleTextColor"];
        [shuoMingAction setValue:[UIColor blackColor] forKey:@"titleTextColor"];
        [cancel setValue:COLORFROMCODE(0x9B9B9B, 1) forKey:@"titleTextColor"];
    }
     [alertController addAction:tiXianAction];
    [alertController addAction:shuoMingAction];
    [alertController addAction:callAction];
    [alertController addAction:cancel];
    [self presentViewController:alertController animated:YES completion:nil];

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, 44)];
    view.backgroundColor = kWhiteColor;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, kScreenWideth, 44)];
    label.textColor = COLORFROMCODE(0x9B9B9B, 1);
    label.font = kFont(14);
    label.text = @"套餐使用明细(次)";
    [view addSubview:label];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RMMHuLiDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.dic = _dataArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
